# Python __new__() 方法详解
class DemoClass:
    instances_created = 0
    def __new__(cls, *args, **kwargs):
        print("__new__():", cls, args, kwargs)
        instance = super().__new__(cls)
        instance.number = cls.instances_created
        cls.instances_created += 1
        return instance

    def __init__(self, attribute):
        print("__init__():", self, attribute)
        self.attribute = attribute

test1 = DemoClass("abc")
test2 = DemoClass("xyz")
print(test1.number, test1.instances_created)
print(test2.number, test2.instances_created)
print()

class NonZero(int):
    def __new__(cls, value):
        return super().__new__(cls, value) if value != 0 else None
    
    def __init__(self, skipped_value):
        # 此例中会跳过此方法
        print("__init__()")
        super().__init__()

print(type(NonZero(-12)))
print(type(NonZero(0)))
